package com.myapp.ruyixing.ui.shopmanager;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.Model;
import com.myapp.ruyixing.ui.BaseActivity;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.LoginHelper;

public class ActivityUpdateStatusActivity extends BaseActivity {

	private TitleBar mTitleBar;
	private View mLoadingView;

	String[] mItems = new String[] { "交易成功", "交易失败" };

	private String mOldStatus;
	private String mOldRemark;
	private String mDataId;
	private String mAId;

	private EditText mStatusView;
	private EditText mRemarkView;

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				Model resul = (Model) o;
				onLoadingDataFinished(true, resul);
			} catch (Exception e) {
				onLoadingDataFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onLoadingDataFinished(false, null);
		}
	};

	private void onLoadingDataStarted(String shopbduss, String aid,
			String dataid, String status, String content) {
		showLoading();
		RYHttpClient.updateActivityOrder(this, mHttpResponseHandler, shopbduss,
				aid, dataid, status, content);
	}

	private void onLoadingDataFinished(boolean succeed, Model result) {
		hideLoading();
		if (succeed && result != null && result.errno == 0) {
			Toast.makeText(this, result.msg, Toast.LENGTH_SHORT).show();
			finish();
		} else {
			Toast.makeText(this, "加载失败" + (result == null ? "" : result.msg),
					Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_shop_manager_activity_update_status);

		initView();

		mDataId = getIntent().getStringExtra("dataid");
		mAId = getIntent().getStringExtra("aid");

		mOldStatus = getIntent().getStringExtra("status");
		mStatusView.setText(mOldStatus);

		mOldRemark = getIntent().getStringExtra("remark");
		mRemarkView.setText(mOldRemark);
	}

	public AlertDialog getMenuDialog(String aTitle, String[] aItems,
			DialogInterface.OnClickListener aListener) {
		return new AlertDialog.Builder(this).setTitle(aTitle)
				.setItems(aItems, aListener).create();
	}

	private void initView() {
		mTitleBar = (TitleBar) findViewById(R.id.title_bar);
		mLoadingView = findViewById(R.id.my_loading);
		mStatusView = (EditText) findViewById(R.id.edit_status);
		mRemarkView = (EditText) findViewById(R.id.message);
		mStatusView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getMenuDialog("请选择状态", mItems,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								mStatusView.setText(mItems[which]);
							}

						}).show();
			}
		});
		mTitleBar.setOnIconClickedListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});

		findViewById(R.id.btn_commit).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				String statusString = mStatusView.getText().toString();
				int status = 0;
				for (int i = 0; i < mItems.length; i++) {
					if (mItems[i].equals(statusString)) {
						status = i + 1;
					}
				}

				if (status == 0) {
					Toast.makeText(ActivityUpdateStatusActivity.this,
							"请修改状态为成功或者失败", Toast.LENGTH_SHORT).show();
					return;
				}

				String remark = mRemarkView.getText().toString();

				onLoadingDataStarted(LoginHelper.getInstance().getShopBduss(),
						mAId, mDataId, status + "", remark);

			}
		});
	}

	private void showTitleBarLoading() {
		mTitleBar.showLoading(true);
	}

	private void hideTitleBarLoading() {
		mTitleBar.showLoading(false);
	}

	private void showLoading() {
		mLoadingView.setVisibility(View.VISIBLE);
	}

	private void hideLoading() {
		mLoadingView.setVisibility(View.GONE);
	}
}
